package worktools.utils;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * Created by 逍遥的熊 on 2017/3/27.
 */
public class NetUtils {

    public interface convert{
        String status = "status";
        String info = "info";
        String header = "header";
        String charset_utf8 = "utf-8";
        String charset_gb2312 = "gb2312";
        String charset_gbg = "gbk";
    }

    public static Map<String,Object> get(String url){
        CookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
        return get(client,url,null,null);
    }

    public static Map<String,Object> get(String url,Map<String,String> params){
        CookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
        return get(client,url,params,null);
    }

    public static Map<String,Object> get(String url,Map<String,String> params, Map<String,String> heads){
        CookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
        return get(client,url,params,heads);
    }

    public static Map<String,Object> post(String url, Map<String,String> params){
        CookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
        return post(client,url,params,null);
    }

    public static Map<String,Object> post(String url, Map<String,String> params, Map<String,String> heads){
        CookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
        return post(client,url,params,heads);
    }

    public static Map<String,Object> post(CloseableHttpClient client,String url, Map<String,String> params, Map<String,String> heads){
        Map<String,Object> resmap = new HashMap<String, Object>();

        Map<String,String> headmap = new HashMap<String, String>();
        HttpPost post = new HttpPost(url);


        if(params != null){
            List<NameValuePair> paramlist = new ArrayList<NameValuePair>();
            for(Map.Entry<String,String> map : params.entrySet()){
                paramlist.add(new BasicNameValuePair(map.getKey(),map.getValue()));
            }
            post.setEntity(EntityBuilder.create().setParameters(paramlist).setContentEncoding(convert.charset_utf8).build());
        }

        if (heads != null){
            for(Map.Entry<String,String> headsmap : heads.entrySet()){
                post.addHeader(headsmap.getKey(),headsmap.getValue());
            }
        }

        try {
            CloseableHttpResponse response = client.execute(post);
            int statusCode = response.getStatusLine().getStatusCode();
            resmap.put(convert.status,statusCode);
            String resinfo = EntityUtils.toString(response.getEntity(),convert.charset_utf8);
            resmap.put(convert.info,resinfo);
            Header[] headers = response.getAllHeaders();
            for (Header header : headers){
                headmap.put(header.getName(),header.getValue());
            }
            resmap.put(convert.header,headmap);

            return resmap;
        }catch (IOException ioex){
            ioex.printStackTrace();
            resmap.put(convert.status,"-1000");
            return resmap;
        }
    }

    public static Map<String,Object> get(CloseableHttpClient client,String url, Map<String,String> params, Map<String,String> heads){

        Map<String,Object> resmap = new HashMap<String, Object>();

        Map<String,String> headmap = new HashMap<String, String>();

        if(params != null){
            StringBuilder sb = new StringBuilder();
            for(Map.Entry<String,String> entry : params.entrySet()){
                sb.append(entry.getKey()+"="+entry.getValue()+"&");
            }
            url = url + sb.substring(0,sb.length()-1);
        }
        try {

            HttpGet httpGet = new HttpGet(url);

            if (heads != null){
                for(Map.Entry<String,String> headsmap : heads.entrySet()){
                    httpGet.addHeader(headsmap.getKey(),headsmap.getValue());
                }
            }


            CloseableHttpResponse response = client.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            resmap.put(convert.status,statusCode);
            String resinfo = EntityUtils.toString(response.getEntity(),convert.charset_utf8);
            resmap.put(convert.info,resinfo);
            Header[] headers = response.getAllHeaders();
            for (Header header : headers){
                headmap.put(header.getName(),header.getValue());
            }
            resmap.put(convert.header,headmap);

            return resmap;
        }catch (IOException ioex){
            ioex.printStackTrace();
            resmap.put(convert.status,"-1000");
            return resmap;
        }
    }
}
